package com.yinhai.message.activemq.listener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yinhai.core.common.api.exception.AppException;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.adapterManager.AdapterManager;
import com.yinhai.message.core.bpo.IMessageBpo;

public class ConsumerMessageListener implements MessageListener {

	private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerMessageListener.class);

//	@Resource(name="adapterManager")
	private AdapterManager adapterManager;

//	@Resource(name="messageBpo")
	private IMessageBpo messageBpo;
	
	@Override
	public void onMessage(Message msg) {
		LOGGER.debug("ActiveMQ 监听");
		ObjectMessage om = (ObjectMessage) msg;
		TMMessageVo amqVo = null;
		try {
			amqVo = (TMMessageVo) om.getObject();
		} catch (JMSException e) {
			LOGGER.error(e.getMessage(),e);
			throw new AppException("从队列中获取消息失败");
		}
		if(amqVo != null){
			String mgid = "";//messageBpo.getSequeue();
			amqVo.getMessage().setMgid(mgid);
			//消息发送处理
			adapterManager.adapter(amqVo);
			//消息持久化处理
			messageBpo.addMessage(amqVo);
		}
	}

}
